from BloodSpiderModel.DjangoResponseTool.response_dict import response_dict, get_page, get_first_error


from Spiders.dailiantong.BSMain import DaiLianTong

from BloodSpiderAPI.apis.dailiantong import utils

from BloodSpiderAPI import models


dlt = DaiLianTong()








# 获取订单列表
def get_order_list(request):
    # return response_dict(data=[

    #     {
    #         "Title": "此单 20 元",
    #         "SerialNo": "10717998089202885420",
    #         "Price": 2,
    #         "Ensure1": 1,
    #         "Ensure2": 1,
    #         "TimeLimit": 5,
    #         "Create": "新手USR20*****704976",
    #         "Game": "王者荣耀",
    #         "Zone": "苹果WX",
    #         "Server": "默认服"
    #     },
    # ])
    price_str = request.GET.get('price_str', "")
    # 从请求中获取参数
    return response_dict(data=utils.filter_order_list(dlt.get_order_list(price_str)["LevelOrderList"]))



# 检查订单是否存在
def check_order_exists(request):
    # 从请求中获取参数
    order_id = request.GET.get('order_id', None)
    if not order_id:
        return response_dict(code=400, message='订单ID不能为空')
    order_detail = dlt.check_order_exists(order_id)
    if order_detail["RecordCount"] == 0:
        return response_dict(code=1, message='订单不存在')
    
    return response_dict(code=0, message='订单存在')
    


# 接收订单
def receive_order(request):
    # 从请求中获取参数
    order_id = request.GET.get('order_id', None)
    if not order_id:
        return response_dict(code=400, message='订单ID不能为空')
    
    payment_password = request.GET.get('payment_password', '128524')
    if not payment_password:
        return response_dict(code=400, message='支付密码不能为空')
    # 接收订单
    order_detail = utils.filter_receive_order(dlt.receive_order(order_id, payment_password))

    return response_dict(code=order_detail["Result"], message=order_detail["Err"])


# 获取订单详情
def get_order_detail(request):
    # 从请求中获取参数
    order_id = request.GET.get('order_id', None)
    if not order_id:
        return response_dict(code=400, message='订单ID不能为空')
    # 获取订单详情
    order_detail = utils.filter_order_detail(dlt.get_order_and_player_info(order_id))
    return response_dict(code=0, message="获取完成", data=order_detail)



# 接受订单
def accept_order(request):
    # 从请求中获取参数
    order_id = request.GET.get('order_id', None)
    # 从请求中获取参数
    payment_password = request.GET.get('payment_password', '128524')
    if not payment_password:
        return response_dict(code=400, message='支付密码不能为空')
    if not order_id:
        return response_dict(code=400, message='订单ID不能为空')
    # 接受订单
    order_detail = utils.filter_accept_order(dlt.receive_order(order_id, payment_password))

    if order_detail["Result"] == 0:
        # 立即调用获取订单详情的函数
        o_detail = utils.filter_order_detail(dlt.get_order_and_player_info(order_id))
        if o_detail["GameAcc"] != "" and o_detail["GamePass"] != "":
            models.OrderListV2.objects.filter(dailiantong_order_id=order_id).update(
                character_name=o_detail["Actor"],
                account_owner=o_detail["GameAcc"],
                account_password=o_detail["GamePass"],
                owner_phone=o_detail["GameMobile"],
                status=2,
            )


    return response_dict(code=order_detail["Result"], message=order_detail["Err"])


# 获取我的个人信息
def get_my_info(request):
    # 获取我的个人信息
    info = dlt.get_my_info()
    return response_dict(code=0, message="获取完成", data=info)
